//切西瓜
//比较均匀，不会有特别大的
function generate(amount, count) {
    let ret = [amount]
    while (count > 1) {
        //挑一个最大的切分
        let cake = Math.max(...ret),
            idx = ret.indexOf(cake),
            part = 1 + Math.floor((cake / 2) * Math.random()),
            rest = cake - part;
        ret.splice(idx, 1, part, rest);
        count--;
    }
    return ret;
}


let amountEl = document.getElementById('amount');
let countEl = document.getElementById('count');
let ul = document.getElementById('result');
let generateBtn = document.getElementById('generateBtn');

generateBtn.onclick = () => {
    let amount = Math.round(parseFloat(amountEl.value * 100));
    let count = parseInt(countEl.value);
    let output = [];
    if (isNaN(amount) || isNaN(count) || amount <= 0 || count <= 0) {
        output.push('输入格式不正确');
    } else if (amount < count) {
        output.push("钱不够分")
    } else {
        output.push(...generate(amount, count))
        output = output.map(m => (m / 100).toFixed(2))
    }
    ul.innerHTML = '<li>' + output.join('</li><li>') + '</li>';
}